<script setup>
const props = defineProps({
  modelValue: { type: Object, default: () => ({}) },
  options: { type: Object, default: () => ({}) },
  disabled: Boolean
})

const emit = defineEmits(['update:modelValue'])

// 表单处理
const formRef = ref()
const form = computed({
  get() {
    return props.modelValue
  },
  set(val) {
    emit('update:modelValue', val)
  }
})
const validate = async () => {
  return new Promise(resolve => {
    formRef?.value.validate((valid, fields) => {
      resolve({ model: 'purchase', name: '采购设置', success: valid, message: fields })
    })
  })
}
const validator = (rule, value, cb) => cb(!value ? new Error('供应商负责人不能为空') : undefined)
const clearValidate = () => {
  formRef?.value?.clearValidate()
}
const resetFields = () => {
  formRef?.value?.resetFields()
}
defineExpose({ validate, clearValidate, resetFields, formRef })
</script>

<template>
  <el-form ref="formRef" :model="form" label-position="top" :disabled="disabled" :label-width="160">
    <div class="grid grid-cols-3 gap-x-3">
      <el-form-item label="供应商类型" prop="purchase_type">
        <VSelect v-model="form.purchase.purchase_type" :options="options.purchase_type" placeholder="请选择" filterable clearable
          class="w-full" />
      </el-form-item>
      <el-form-item label="是否验厂" prop="check_factory">
        <VSelect v-model="form.purchase.check_factory" :options="options.check_factory" placeholder="请选择" class="w-full"
          @change="form.purchase.factory_file = []" />
      </el-form-item>
    </div>
    <div v-if="form.purchase.check_factory === 1" class="mb-3">
      <VUpload v-model="form.purchase.factory_file" title="验厂文件" />
    </div>
    <div class="grid grid-cols-3 gap-x-3">
      <el-form-item label="是否贴标" prop="is_label">
        <VSelect v-model="form.purchase.is_label" :options="options.is_label" placeholder="请选择" filterable clearable
          class="w-full" />
      </el-form-item>
      <el-form-item label="支持退换货" prop="is_return">
        <VSelect v-model="form.purchase.is_return" :options="options.is_return" placeholder="请选择" filterable clearable
          class="w-full" />
      </el-form-item>
      <el-form-item label="送货清单" prop="deliver_list">
        <VSelect v-model="form.purchase.deliver_list" :options="options.deliver_list" placeholder="请选择" filterable clearable
          class="w-full" />
      </el-form-item>
      <el-form-item label="支持工厂直发" prop="factory_deliver">
        <VSelect v-model="form.purchase.factory_deliver" :options="options.is_label" placeholder="请选择" filterable clearable
          class="w-full" />
        <el-checkbox v-model="form.purchase.is_factory_deliver_update" :true-value="2" :false-value="1">修改审核通过后更新该供应商所有产品的是否支持工厂直发</el-checkbox>
      </el-form-item>
      <el-form-item label="支持备料"  prop="factory_deliver">
        <VSelect v-model="form.purchase.is_demand" :options="options.is_demand" placeholder="请选择" class="w-full" /> 
        <el-checkbox v-model="form.purchase.is_demand_update" :true-value="2" :false-value="1">修改审核通过后更新该供应商所有产品的是否支持备料</el-checkbox>
      </el-form-item>
      <el-form-item label="扣减天数" prop="factory_deliver">
        <el-input-number v-model="form.purchase.abatement_day" controls-position="right" :min="0" :step="1" :precision="0"
          placeholder="请输入" class="w-full" />
        <el-checkbox v-model="form.purchase.is_abatement_day_update" :true-value="2"
          :false-value="1">修改审核通过后更新该供应商所有产品的扣减天数</el-checkbox>
      </el-form-item>
      <el-form-item label="支持更换主体" prop="change_main">
        <VSelect v-model="form.purchase.change_main" :options="options.is_label" placeholder="请选择" filterable clearable
          class="w-full" />
      </el-form-item>
      <el-form-item label="海外仓采购" prop="oversea_purchase_id">
        <VCascader v-model="form.purchase.oversea_purchase_id" placeholder="请选择" filterable clearable class="w-full"
          @change="({ labels }) => form.purchase.oversea_purchase = labels" />
      </el-form-item>
      <el-form-item label="FBA采购" prop="fba_purchase_id">
        <VCascader v-model="form.purchase.fba_purchase_id" placeholder="请选择" filterable clearable class="w-full"
          @change="({labels}) => form.purchase.fba_purchase = labels" />
      </el-form-item>
      <el-form-item label="供应商负责人" prop="basic.director_id"
        :rules="[{ required: true, message: '供应商负责人不能为空', trigger: 'change' }, { validator }]">
        <VCascader v-model="form.basic.director_id" placeholder="人员" class="w-full"
          @change="({ labels }) => form.basic.director_name = labels" />
      </el-form-item>
      <el-form-item label="国内物流运输天数" prop="factory_deliver">
        <el-input-number v-model="form.purchase.logistics_day" controls-position="right" :min="0" :step="1" :precision="0"
          placeholder="请输入" class="w-full" />
      </el-form-item>
      <el-form-item label="是否支持辅料加工" prop="is_accessories">
        <VSelect v-model="form.purchase.is_accessories" placeholder="请选择" :options="options.is_accessories" class="w-full" />
      </el-form-item>
  </div>
</el-form></template>
